#include "sprinter.h"
#include "../weapon/pfire.h"

#include <OgreEntity.h>
#include <stdio.h>
#include <OgreMath.h>
#include <OgreVector3.h>
#include "OgreParticleSystem.h"
#include <OgreParticleEmitter.h>

Sprinter::Sprinter(World *world): BaseEnemy(world)
{
    createModel();
    m_node->scale(1.3,1.3,1.3);

    m_speed = 500;
    //m_lastShoot = m_world->getCurrentTime();
    m_health = 20;
}

void Sprinter::move(float time){
    Ogre::Vector3 diff = m_world->getPlayer()->getPosition() - m_node->getPosition();

    m_node->lookAt(m_world->getPlayer()->getPosition(), Ogre::Node::TS_PARENT, Ogre::Vector3::UNIT_Z);

    if(m_world->getCurrentTime() - m_lastShoot > 300){
        Projectile *projectile= new PFire(m_world, 1000, 100, -diff.getRotationTo(Ogre::Vector3(0,0,1)).getYaw().valueDegrees());
        projectile->setDamage(3);
        m_world->addActor(projectile, m_node->getPosition());
        m_lastShoot = m_world->getCurrentTime();
    }

    diff.normalise();
    diff *= m_speed;

    m_node->setPosition(m_node->getPosition() + time* diff);
}

void Sprinter::animDeath(Ogre::Vector3 lastPos){
    Ogre::SceneManager *scene = m_world->getSceneManager();

    // create particle system and attach it to d_node at death position
    Ogre::ParticleSystem* splatter = scene->createParticleSystem(25);
    splatter->setMaterialName("Examples/Flare");
    splatter->setDefaultDimensions(30, 30);

    Ogre::ParticleEmitter* emitter = splatter ->addEmitter("Point");  // add a point emitter

    // set the emitter properties
    emitter->setAngle(Ogre::Degree(180));
    emitter->setMinTimeToLive(0.5);
    emitter->setMaxTimeToLive(2);
    emitter->setDuration(0.1);
    emitter->setEmissionRate(1000);
    emitter->setParticleVelocity(200);
    emitter->setDirection(Ogre::Vector3::NEGATIVE_UNIT_Z);
    emitter->setColour(Ogre::ColourValue::Green);

    Ogre::SceneNode *d_node = scene->getRootSceneNode()->createChildSceneNode();
    d_node->setPosition(lastPos);
    d_node->attachObject(splatter);
}
